package edu.gatech.bsbmule.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

import edu.gatech.bsbmule.util.SaveAndLoadManager;

/**
 * Main window which dynamically switches JPanels
 * 
 * Uses singleton here
 * 
 * @author BSB
 * @version 1.0 10/6/2013
 * 
 */
public class MainWindow extends JFrame {

	private JMenuItem mntmPause;
	private JMenuItem mntmSave;
	
	private static MainWindow instance = null;
	
	public static MainWindow getInstance() {
		if (instance == null) {
			instance = new MainWindow();
		}
		return instance;
	}
	
	/**
	 * Create the frame.
	 */
	
	private MainWindow() {
				
		setTitle("M.U.L.E by BSB");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnGame = new JMenu("Game");
		menuBar.add(mnGame);

		mntmPause = new JMenuItem("Pause");
		mnGame.add(mntmPause);
		
		mntmSave = new JMenuItem("Save");
		mntmSave.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				//Json saving
				final JFileChooser chooser = new JFileChooser();
				FileNameExtensionFilter filter
					= new FileNameExtensionFilter("json file", "json");
				chooser.addChoosableFileFilter(filter);
				chooser.setFileFilter(filter);
				int returnVal = chooser.showSaveDialog(MainWindow.this);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					FileWriter writer = null;
					try
					{
						File file 
							= new File(chooser.getSelectedFile().getCanonicalPath()
							+ "." + filter.getExtensions()[0]);
						writer = new FileWriter(file);
						writer.write(SaveAndLoadManager.save());
					} catch (IOException e1) {
						System.err.println("Error saving file");
					} finally {
						if (writer != null) {
							try {
								writer.close();
							} catch (IOException e2) {
								System.err.println("Error saving file");
							}
						}
					}
				}
			}
		});
		mnGame.add(mntmSave);
		
		JMenuItem mntmQuit = new JMenuItem("Quit");
		mntmQuit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mnGame.add(mntmQuit);

		JMenu mnAbout = new JMenu("About");
		menuBar.add(mnAbout);

		JMenuItem mntmInstructions = new JMenuItem("Instructions");
		mnAbout.add(mntmInstructions);

		JMenuItem mntmAbout = new JMenuItem("About");
		mnAbout.add(mntmAbout);
	}

	public void setPanel(JPanel panel) {
		setContentPane(panel);
		revalidate();
		
	}

	public void setPauseItemEnable(boolean enabled) {
		mntmPause.setEnabled(enabled);
	}
	
	public void setSaveItemEnable(boolean enabled) {
		mntmSave.setEnabled(enabled);
	}
	
}
